{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  肖丽雯\n",
    "+ 学号  0164513"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狼烟风沙口\n",
      "还请将军少饮酒\n",
      "前方的路不好走\n",
      "我在家中来等候\n",
      "可愿柳下走\n",
      "满头杨花共白首\n",
      "十两相思二两酒\n",
      "我才把爱说出口\n",
      "\n",
      "狼烟风沙口\n",
      "还请将军少饮酒\n",
      "前方的路不好走\n",
      "我在家中来等候\n",
      "可愿柳下走\n",
      "满头杨花共白首\n",
      "十两相思二两酒\n",
      "我才把爱说出口\n",
      "\n",
      "他一路凶险一路漂泊\n",
      "从未安静过活\n",
      "也受过挫折依旧执着\n",
      "也曾信仰过佛\n",
      "她风月佳人傲骨温存\n",
      "似桃花般撩人\n",
      "也曾有座坟住着亡人\n",
      "她说她不信神\n",
      "可她等了有三载\n",
      "她等到碧落沧海\n",
      "她等到四季交换\n",
      "她依旧死不悔改\n",
      "她口口声声风和雪中\n",
      "只手掌着孤灯\n",
      "她醉醉疯疯渺渺空空\n",
      "终日敲着佛钟\n",
      "她诵经她敲钟\n",
      "她扑空她成疯\n",
      "水成冰风无声\n",
      "雪冻松她寿终\n",
      "她最后也没等到他\n",
      "他最后也没回家\n",
      "她见到一朵彼岸花\n",
      "她却说那好像他\n",
      "我的将军啊\n",
      "你究竟去了哪啊\n",
      "你说你去把敌杀啊\n",
      "何故你不回家\n",
      "我的英雄啊\n",
      "你爱上别人了吗\n",
      "如果是的告诉我\n",
      "让我随风飘吧\n",
      "\n",
      "狼烟风沙口\n",
      "还请将军少饮酒\n",
      "前方的路不好走\n",
      "我在家中来等候\n",
      "可愿柳下走\n",
      "满头杨花共白首\n",
      "十两相思二两酒\n",
      "我才把爱说出口\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def song():\n",
    "    print(\"狼烟风沙口\\n还请将军少饮酒\\n前方的路不好走\\n我在家中来等候\\n可愿柳下走\\n满头杨花共白首\\n十两相思二两酒\\n我才把爱说出口\\n\")\n",
    "song()\n",
    "song()\n",
    "print(\"他一路凶险一路漂泊\\n从未安静过活\\n也受过挫折依旧执着\\n也曾信仰过佛\\n她风月佳人傲骨温存\\n似桃花般撩人\\n也曾有座坟住着亡人\\n她说她不信神\\n可她等了有三载\\n她等到碧落沧海\\n她等到四季交换\\n她依旧死不悔改\\n她口口声声风和雪中\\n只手掌着孤灯\\n她醉醉疯疯渺渺空空\\n终日敲着佛钟\\n她诵经她敲钟\\n她扑空她成疯\\n水成冰风无声\\n雪冻松她寿终\\n她最后也没等到他\\n他最后也没回家\\n她见到一朵彼岸花\\n她却说那好像他\\n我的将军啊\\n你究竟去了哪啊\\n你说你去把敌杀啊\\n何故你不回家\\n我的英雄啊\\n你爱上别人了吗\\n如果是的告诉我\\n让我随风飘吧\\n\")\n",
    "song()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)\n",
      "均值为 6.0\n",
      "方差为 11.0\n",
      "个数为 11\n",
      "\n",
      "\n",
      "number (2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 13, 15)\n",
      "均值为 6.25\n",
      "方差为 19.477272727272727\n",
      "个数为 12\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def statistic(*number):\n",
    "    n=0\n",
    "    sum=0\n",
    "    s=0\n",
    "    for var in number:\n",
    "        n=n+1\n",
    "        sum=sum+var\n",
    "    mean=sum/n\n",
    "    for var in number:\n",
    "        s=s+(var-mean)**2\n",
    "    print(\"number\",number)\n",
    "    print(\"均值为\",mean)\n",
    "    print(\"样本方差为\",s/(n-1))\n",
    "    print(\"个数为\",n)\n",
    "    print(\"\\n\")\n",
    "statistic(1,2,3,4,5,6,7,8,9,10,11)\n",
    "statistic(2,3,4,5,6,7,8,9,1,2,13,15)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)\n",
      "均值为 6.0\n",
      "个数为 11\n",
      "样本标准差为 3.3166247903554\n",
      "\n",
      "\n",
      "number (2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 13, 15)\n",
      "均值为 6.25\n",
      "个数为 12\n",
      "样本方差为 19.477272727272727\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def statistic(*number,variance=None,std=None):\n",
    "    n=0\n",
    "    sum=0\n",
    "    s=0\n",
    "    for var in number:\n",
    "        n=n+1\n",
    "        sum=sum+var\n",
    "    mean=sum/n\n",
    "    for var in number:\n",
    "        s=s+(var-mean)**2\n",
    "    print(\"number\",number)\n",
    "    print(\"均值为\",mean)\n",
    "    print(\"个数为\",n)\n",
    "    if variance:\n",
    "        print(\"样本方差为\",s/(n-1))\n",
    "    if std:\n",
    "        print(\"样本标准差为\",pow(s/(n-1),1/2))\n",
    "    print(\"\\n\")        \n",
    "statistic(1,2,3,4,5,6,7,8,9,10,11,std=1)\n",
    "statistic(2,3,4,5,6,7,8,9,1,2,13,15,variance=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)\n",
      "均值为 6.0\n",
      "个数为 11\n",
      "样本标准差为 3.3166247903554\n",
      "均值标准误为 1.0\n",
      "最大值为 11\n",
      "\n",
      "\n",
      "number (2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 13, 15)\n",
      "均值为 6.25\n",
      "个数为 12\n",
      "样本方差为 19.477272727272727\n",
      "最小值为 1\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def statistic(*number,variance=None,std=None,sem=None,ma=None,mi=None):\n",
    "    n=0\n",
    "    sum=0\n",
    "    s=0\n",
    "    for var in number:\n",
    "        n=n+1\n",
    "        sum=sum+var\n",
    "    mean=sum/n\n",
    "    for var in number:\n",
    "        s=s+(var-mean)**2\n",
    "    print(\"number\",number)\n",
    "    print(\"均值为\",mean)\n",
    "    print(\"个数为\",n)\n",
    "    if variance:\n",
    "        print(\"样本方差为\",s/(n-1))\n",
    "    if std:\n",
    "        print(\"样本标准差为\",pow(s/(n-1),1/2))\n",
    "    if sem:\n",
    "        print(\"均值标准误为\",pow(s/(n*(n-1)),1/2))\n",
    "    if ma:\n",
    "        print(\"最大值为\",max(number))\n",
    "    if mi:\n",
    "        print(\"最小值为\",min(number))\n",
    "    print(\"\\n\")        \n",
    "statistic(1,2,3,4,5,6,7,8,9,10,11,std=1,sem=1,ma=1)\n",
    "statistic(2,3,4,5,6,7,8,9,1,2,13,15,variance=1,mi=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
